<template>
  <div class="agents-edit">
    <el-dialog v-bind="$attrs" :title="state.dialogTitle" v-model="state.modalVisible" :before-close="cancelDialog"
      :append-to-body="true" :destroy-on-close="true" :close-on-click-modal="false" :close-on-press-escape="false"
      :lock-scroll="false" class="agents-dialog" draggable>
      <el-form ref="agentsRef" :model="state.form" :rules="state.rules" label-position="top">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="代理商名称" prop="agentsname">
              <el-input v-model="state.form.agentsname" placeholder="代理商名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代理商类型" prop="agentstype">
              <el-input v-model="state.form.agentstype" placeholder="代理商类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代理商联系人" prop="agentslxr">
              <el-input v-model="state.form.agentslxr" placeholder="代理商联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代理商联系电话" prop="agentslxdh">
              <el-input v-model="state.form.agentslxdh" placeholder="代理商联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代理商联系地址" prop="agentslxdz">
              <el-input v-model="state.form.agentslxdz" placeholder="代理商联系地址" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="代理商审核人" prop="agentsshr">
              <el-input v-model="state.form.agentsshr" placeholder="代理商审核人" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="代理商账户金额" prop="agentsprice">
              <el-input v-model="state.form.agentsprice" placeholder="代理商账户金额" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="代理商审核状态" prop="agentsshstatus">
              <el-input v-model="state.form.agentsshstatus" placeholder="代理商审核状态" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代理商审核时间" prop="agentsshdate">
              <el-input v-model="state.form.agentsshdate" placeholder="代理商审核时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代理商审核备注" prop="agentsshbz">
              <el-input v-model="state.form.agentsshbz" placeholder="代理商审核备注" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="代理商是否可用" prop="agentstatus">
              <el-input v-model="state.form.agentstatus" placeholder="代理商是否可用" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="代理商创建时间" prop="agentscreatedate">
              <el-input v-model="state.form.agentscreatedate" placeholder="代理商创建时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代理商创建人" prop="agentscreateuser">
              <el-input v-model="state.form.agentscreateuser" placeholder="代理商创建人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代理商信息最后更新时间" prop="agentsupdate">
              <el-input v-model="state.form.agentsupdate" placeholder="代理商信息最后更新时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代理商信息最后更新人" prop="agentsupuser">
              <el-input v-model="state.form.agentsupuser" placeholder="代理商信息最后更新人" />
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import useAgentsManageManageStore from '@store/modules/agentsManage'
import { filterObject } from '@/utils/index'
import { nextTick } from 'vue';

const agentsManageStore = useAgentsManageManageStore()
const { proxy } = getCurrentInstance()
const agentsRef = ref(null)

const state = reactive({
  modalVisible: false,
  dialogTitle: '',
  editView: {},
  form: {
    agentsname: '',
    agentslxr: '',
    agentslxdh: '',
    agentslxdz: '',
    // agentsshr: '',
    agentsprice: '',
    // agentsshstatus: '',
    // agentsshdate: '',
    // agentsshbz: '',
    agentstatus: '',
    // agentscreatedate: '',
    // agentscreateuser: '',
    // agentsupdate: '',
    // agentsupuser: '',
  },
  rules: {
    // account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
    // username: [{ required: true, message: '姓名/昵称不能为空', trigger: 'blur' }],
    // userworkgroup1: [{ required: true, message: '关联公司不能为空', trigger: 'blur' }],
    // userworkgroup2: [{ required: true, message: '关联部门不能为空', trigger: 'blur' }],
  },
})

const emits = defineEmits(['refresh'])

const getDataView = (agentsid) => {
  nextTick(async () => {
    const result = await agentsManageStore.view({ "agentsid": agentsid })
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
  })
}

const submitDialog = () => {
  agentsRef.value.validate(async (valid) => {
    if (valid) {
      if (Object.keys(state.editView).length) {
        state.form.agentsid = state.editView.agentsid
        await agentsManageStore.up(state.form)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        await agentsManageStore.add(state.form)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      agentsRef.value.resetFields()
      emits('refresh')
      state.modalVisible = false
    }
  })
}

const cancelDialog = () => {
  state.editView = {}
  agentsRef.value.resetFields()
  state.modalVisible = false
}

const openDialog = async (data = {}) => {
  if (Object.keys(data).length) await getDataView(data.agentsid)
  state.modalVisible = true
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.agentsname}】` : '新增'
}

defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
